<?php
    session_start();
    require_once('userlevel.php'); //Require/include the permissions file
    require_once('template.php'); //Require/include the functions file
    require_once('functions.php'); //Require/include the template file
    require_once('db.php'); //Require/include the file with the database connection information
     
    if (!$_SESSION['user_loggedin']) {
        die ("Not logged in... <script>document.location.href='login.php'</script>");
    } elseif (!($_SESSION['userlevel'] >= $userlevel['main'])) {
        die (errorpage ("Your user level is not high enough to use this feature!", 'Tracked files'));
    } else {
        //Connect to to the database using values provided by db.php
        $db = new my_db;
         
        $dir = strip_tags(mysql_escape_string($_GET['dir']));
        template_headtag("List of all files in $dir");
        template_header();
        template_left();
        echo "<div class='pageheadertext'>List of all files in</div>";
        echo "<div class='subheadertext'>$dir</div><br/>";
        echo "<div align='center'>";

        $db->query("SELECT DISTINCT filepath FROM trackedfiles WHERE filepath LIKE '$dir%/' ORDER BY filepath");
        if ($db->nf() > 0 AND !empty($dir)) {
            $curdirlength = count(explode('/', $dir));
            $dirarray = Array();
            while ($db->next_record()) {
                $dir1 = strip_tags($db->f("filepath"));
                if ($dir1 !== $dir) {
                    $dir1array = explode('/', $dir1);
                    $dirarray[] = $dir1array[$curdirlength-1];
                }
            }
            $dirarray = array_unique($dirarray);
            echo "<div class='subheadertext2'>Folders</div>";
            echo "<table class='table'>";
            fileicon('dir');
            foreach($dirarray AS $dir1) {
                 
                if ($rowstyle == 'oddrow') {
                    $rowstyle = 'evenrow';
                } else {
                    $rowstyle = 'oddrow';
                }
                echo "<tr class='$rowstyle'>";
                echo "<td>";
                echo "<img src='images/mimetypes/$icon' width='16' height='16' border='0' alt='$desc' title='$desc' />";
                echo "</td>";
                echo "<td  width='100%'>";
                echo "<a href='listfiles.php?dir=$dir$dir1/'>$dir1</a>";
                echo "</td>";
                if ($_SESSION['userlevel'] >= $userlevel['stoptrackingfile']) {
                    echo "<td>";
                    echo "<a href='stoptrackingdir.php?dir=".urlencode("$dir$dir1/")."' title='Stop tracking directory'><img src='images/tables/stop.png' width='16' height='16' border='0' alt='Stop tracking directory' /></a>";
                    echo "</td>";
                }
                echo "</tr>\n";
            }
            echo "</table><br/>";
        }
                
        $db->query("SELECT * FROM trackedfiles WHERE filepath='$dir' ORDER BY filename");
        if ($db->nf() > 0) {
            echo "<div class='subheadertext2'>Files</div>";

            echo "<table class='table'>";

            if ($dir !== '/') {
                $dirparts = explode('/', $dir);
                array_pop($dirparts);
                array_pop($dirparts);
                $updir = implode('/', $dirparts).'/';
                if (empty($updir)) {
                    $updir = '/';
                }
                $rowstyle = 'oddrow';
                echo "<tr class='$rowstyle'>
                    <td><a href='listfiles.php?dir=$updir' title='Up one level'><img src='images/mimetypes/up.png' width='16' height='16' border='0' alt='Up one level' /></a></td>
                    <td width='100%'><a href='listfiles.php?dir=$updir' title='Up one level'>.. (up one level)</a></td>
                    <td>View</td>
                    <td>History</td>
                    <td>Update</td>
                    <td>Location</td>
                    <td>Track</td>
                    </tr>
                    ";
            }
            // keep getting the next row until there are no more to get
            while ($db->next_record()) {
                if ($rowstyle == 'oddrow') {
                    $rowstyle = 'evenrow';
                } else {
                    $rowstyle = 'oddrow';
                }
                 
                // Print out the contents of each row into a table
                $fileid = strip_tags($db->f('id'));
                $latestrev = strip_tags($db->f('latestrev'));
                $filename = strip_tags($db->f('filename'));
                $filetype = strip_tags($db->f('filetype'));
                $filearray = explode('.', $filename);
                if (count($filearray) > 1) {
                    $ep = count($filearray) -1;
                    $extension = $filearray[$ep];
                } else {
                    $extension = '';
                }
                fileicon($extension);
                 
                echo "<tr class='$rowstyle'>";
                echo "<td>";
                echo "<img src='images/mimetypes/$icon' width='16' height='16' border='0' alt='$desc' title='$desc' />";
                echo "</td>";
                echo "<td>";
                if ($_SESSION['userlevel'] >= $userlevel['displayfile']) {
                    echo "<a href='viewfile.php?id=$fileid' title='View current file'>$filename</a>";
                } else {
                    echo $filename;
                }
                echo "</td>";
                if ($_SESSION['userlevel'] >= $userlevel['displayfile']) {
                    echo "<td>";
                    echo "<center><a href='view.php?id=$fileid' title='View file'><img src='images/tables/view.png' width='16' height='16' border='0' alt='View' /></a></center>";
                    echo "</td>\n";
                }
                if ($_SESSION['userlevel'] >= $userlevel['history']) {
                    echo "<td>";
                    echo "<center><a href='history.php?id=$fileid' title='History'><img src='images/tables/history.png' width='16' height='16' border='0' alt='History' /></a></center>";
                    echo "</td>\n";
                }
                if ($_SESSION['userlevel'] >= $userlevel['updatefile']) {
                    echo "<td>";
                    echo "<center><a href='update.php?id=$fileid' title='Update file'><img src='images/tables/update.png' width='16' height='16' border='0' alt='Update file' /></a></center>";
                    echo "</td>\n";
                }
                if ($_SESSION['userlevel'] >= $userlevel['renamefile']) {
                    echo "<td>";
                    echo "<center><a href='updatename.php?id=$fileid' title='Update the location of file'><img src='images/tables/updatename.png' width='16' height='16' border='0' alt='Change Location' /></a></center>";
                    echo "</td>\n";
                }
                if ($_SESSION['userlevel'] >= $userlevel['stoptrackingfile']) {
                    echo "<td>";
                    echo "<center><a href='stoptrackingfile.php?id=$fileid' title='Stop tracking file'><img src='images/tables/stop.png' width='16' height='16' border='0' alt='Stop tracking file' /></a></center>";
                    echo "</td>\n";
                }
                echo "</tr>\n";
            }
            echo "</table>";
        } else {
            if (is_dir($dir)) {
                echo "<table class='table'>";
                 
                if ($dir !== '/') {
                    $dirparts = explode('/', $dir);
                    array_pop($dirparts);
                    array_pop($dirparts);
                    $updir = implode('/', $dirparts).'/';
                    if (empty($updir)) {
                        $updir = '/';
                    }
                    echo "<tr class='oddrow'>
                        <td  width='16px'><a href='listfiles.php?dir=$updir'><img src='images/mimetypes/up.png' width='16' height='16' border='0' alt='up' /></a></td>
                        <td width='200px'><a href='listfiles.php?dir=$updir'>..</a></td>
                        </tr>\n
                        ";
                }
                echo "<tr class='even'>
                     
                    <td colspan='2'>";
                echo "<br />No files found here!<br /><br />Track a <a href='addfile.php'>file</a> or a <a href='adddir.php'>directory</a>.<br /><br /><br />";
                 
                echo "</td>
                    </tr>
                    ";
                 
                 
                echo "</table>";
                 
            } else {
                errorbox('Not a vaild directory!');
            }
        }
         
        echo "</div><br/>";
        template_footer();
    }
?>
